package com.xxxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xxxy.fallback.SentinelFallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sentinel")
public class SentinelController {
    @GetMapping("/slowRequest")
    @SentinelResource(value = "slowRequestResource", fallback = "sentinelFallback",
            fallbackClass = SentinelFallback.class)
    public String slowRequest() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        String msg = "进入慢调用方法";
        System.out.println(msg);
        return  msg;
    }

    @GetMapping("/errorRate")
    @SentinelResource(value = "errorRateResource", fallback = "sentinelFallback",
            fallbackClass = SentinelFallback.class)
    public String errorRate() {
        String msg = "进入异常比例方法";
        System.out.println(msg);
        int m = 5/0;
        return  msg;
    }

    @GetMapping("/errorCount")
    @SentinelResource(value = "errorCountResource", fallback = "sentinelFallback",
            fallbackClass = SentinelFallback.class)
    public String errorCount() {
        String msg = "进入异常数方法";
        System.out.println(msg);
        int m = 5/0;
        return  msg;
    }
}
